#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require


#include "../../../../core/shader_binding_ID.h"
#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"



layout(location=0) out vec4 outColor;


layout(set = 1, binding = DEF_BINDING_WIDGET_Texture) uniform sampler2D texColor[];

layout(set = 2, binding = DEF_BindID_TextureCube) uniform samplerCube texEnv[];
layout(set = 2, binding = DEF_BindID_TextureShadow) uniform sampler2D texShadow[];
layout(set = 2, binding = DEF_BindID_TexCubeShadow) uniform samplerCube texCubeShadow[];
layout(set = 2, binding = DEF_BindID_GBufferImage)  uniform sampler2D GBuffer_Image[];

layout(set = 2, binding = DEF_BindID_CompositingShadow) uniform sampler2D compositing_shadow[];


layout(std140, set = 2, scalar, binding = DEF_BindID_GlobalTextAtt) uniform Buf0 {
	S_GlobalAtt_RasterPostCompositing m_constPush;
};

//每个灯光的阴影图层数量
layout(std430, set = 2, column_major, scalar, binding = DEF_BindID_TexShadow_LightIndex) readonly buffer Buf1 {
	S_LightIndex m_LightIndex[];
};

//光投影矩阵
layout(std430, set = 2, column_major, scalar, binding = DEF_BindID_TexShadow_PVMatrix) readonly buffer Buf3 {
	mat4 m_LightArea_PVMatrix[];
};

//区域光属性
layout(std430, set = 2, column_major, scalar, binding = DEF_BindID_TexShadow_AreaLight) readonly buffer Buf2 {
	S_LightArea m_LightArea[];
};

//材质属性
layout(std430, set = 2, scalar, column_major, binding = DEF_BINDING_TasterBindID_MaterialID) readonly buffer Buf4 {
	S_Material gMaterial[];
};




layout(location = 0) in vec3 inEyeDir;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec4 inVert;
layout(location = 7) in flat uint inMaterialID;
layout(location = 8) in flat float inElementID;
layout(location = 9) in float inUV;

layout(early_fragment_tests) in;


#include "../光栅渲染/灯光阴影.glsl"
#include "../光栅渲染/纹理模糊计算.glsl"



const vec3 w = vec3(0.2125,0.7154,0.0721);

void main(void) {
	float stepSample = max(m_constPush.m_resolutionRatio.x, m_constPush.m_resolutionRatio.y);
	//vec2 sampleOffsetSize = vec2(1.0) / stepSample;
	vec2 invResolution = 1.0 / m_constPush.m_resolutionRatio;

	vec2 screenCoords = vec2(gl_FragCoord.xy) / m_constPush.m_resolutionRatio;
	outColor = vec4(0.2,0.0,0.0,1);
	//return;
	vec3 rv = reflect(normalize(-inEyeDir), inNormal);
	
	S_Material mate = gMaterial[inMaterialID];
	
	/*vec3 irradiance = vec3(0);
	vec3 albedo = vec3(0.4,0.2,0.1);
	vec3 metallicRoughness = vec3(0.1);
	vec3 F0 = vec3(0.04);

	//切线法线
	vec3 N = normalize(inNormal);
	vec3 R = reflect(normalize(inEyeDir), N);
	float VD = 1 / length(inEyeDir);

	//环境纹理
	vec4 env = texture(texEnv[0], N, 0);
	irradiance.rgb = env.rgb;
	
	
	vec4 env_s = texture(texEnv[1], rv, 0);
	irradiance += vec3(env_s.rgb * env_s.a) * mate.m_reflect;

	//视线高光
	float vs = max(dot(normalize(-inEyeDir), inNormal), 0);
	vs = pow(vs, 60)* VD;
	irradiance += mate.m_specular * vs;
	irradiance *= m_constPush.m_envIntensity;


	vec3 lightColor = vec3(0.001);*/
	
	
	//outColor.xyz = ((irradiance * (1-mate.m_reflect) + ) * lightColor) * s;
	outColor.xyz = (mate.m_color);
	return;
}


